//
//  MainViewController.m
//  HGMMT
//
//  Created by huan liu on 2018/12/29.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

#import "MainViewController.h"
#import "LinkViewController.h"
#import "LYYNavigationController.h"
#import "LinkServers.h"



#import "UIView+ViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self receiveMessage];
    
  //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notActionDisconnect:) name:Link_Disconnect object:nil];
    
}


#pragma mark -通知相关
-(void)notActionDisconnect:(NSNotification *)not{
    
    UIAlertController *alerVC =[UIAlertController alertControllerWithTitle:@"提示" message:@"连接已经断开,请重新进行连接" preferredStyle:(UIAlertControllerStyleAlert)];
    [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        LinkViewController *link = [[LinkViewController alloc]init];
        LYYNavigationController *nav = [[LYYNavigationController alloc]initWithRootViewController:link];
        keyWindow.rootViewController = nav;
        
    }]];
    
    [self presentViewController:alerVC animated:YES completion:nil];
    
}

-(void)dealloc{
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark -接收消息
-(void)receiveMessage{
    
    LinkServers *server = [LinkServers shareInstance];
    
    server.linkBlock = ^(BOOL isSuccess) {
        
        if (isSuccess == NO) {
            
            
            
            
        }
        
    };
    
    
    
    server.communBlock = ^(NSData *date) {
        
        NSString *repond =  [[NSString alloc] initWithBytes:date.bytes length:date.length encoding:NSUTF8StringEncoding];
        
        //int a = 15;
        
        /*
         0002:{"id":1,"name":"test","nameCN":"","dept":"caac","version":"v0.1","frame":"ssl","root":"http://10.10.211.189:8181/HGMIDE/plugin/test"}
         */
        /*
         0005:-L:[{"id":1,"guid":"123456789","usercode":"2853135","username":"邢伟新","dept":"数据中心"}]
         */
        
        /*
         0006:-L:[{"id":1,"name":"APIDemo","nameCN":"插件测试","dept":"数据中心","version":"1","frame":"OC","root":"http://10.10.211.119:8181/HGMIDE/plugin/APIDemo"},{"id":2,"name":"wxTest","nameCN":"邢伟新测试","dept":"数据中心","version":"1","frame":"oc","root":"http://10.10.211.119:8181/HGMIDE/plugin/wxTest"}]
         */
        
        NSRange range1 = NSMakeRange(0, 4);
        NSString *type = [repond substringWithRange:range1];
        
        if ([type isEqualToString:@"0002"]) {
            
            NSString *content = [repond substringFromIndex:5];
            NSDictionary *dicx = [Tool dictionaryWithJsonString:content];
           HGPluginViewController *HGPlugin = [[HGPluginViewController alloc]init];
            PluginModel *model = [[PluginModel alloc]initWithDic:dicx];
            [HGPlugin startPageSetting:model];
            
            
            NSInteger index =  self.tabBarController.selectedIndex;

            NSArray *array = self.tabBarController.viewControllers;
            NSMutableArray *mutaArray = [NSMutableArray array];
            
            for (LYYNavigationController *lyy in array) {
                
                UIViewController *vc = lyy.viewControllers[0];
                
                [mutaArray addObject:vc];
                
            }
            
            MainViewController *mv = [mutaArray objectAtIndex:index];
            [mv.navigationController pushViewController:HGPlugin animated:YES];
            
            
            
        }
        else if ([type isEqualToString:@"0005"]){
            
            NSString *content = [repond substringFromIndex:8];
           // NSArray *array = [Tool dictionaryWithJsonString:content];
            NSData *userdata = [content dataUsingEncoding:NSUTF8StringEncoding];
            NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:userdata options:(NSJSONReadingMutableContainers) error:nil];
           
            NSDictionary *userInfo;
            
            if (jsonArray.count > 0) {
                
               userInfo  = @{
                             @"state":[NSNumber numberWithBool:YES],
                             @"userinfo":jsonArray
                            };
                
            }else{
                
                userInfo  = @{
                              @"state":[NSNumber numberWithBool:NO],
                              };
            }

          //发送通知给userInfo界面
            [[NSNotificationCenter defaultCenter]postNotificationName:GetUserInfo_not object:self userInfo:userInfo];
            
        }
        else if ([type isEqualToString:@"0006"])
        {
            NSString *content = [repond substringFromIndex:8];
            // NSArray *array = [Tool dictionaryWithJsonString:content];
            NSData *userdata = [content dataUsingEncoding:NSUTF8StringEncoding];
            NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:userdata options:(NSJSONReadingMutableContainers) error:nil];

            HGLog(@"我获取到了+++++++++++++++++++++");
            
            NSArray *array = self.tabBarController.viewControllers;
            MainViewController *vc = ((LYYNavigationController *)array[1]).viewControllers[0];
            [vc getPluginList:jsonArray];
            
        }
        
    };
    
}




#pragma mark - 视图控制器的方法
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     self.navigationController.navigationBar.hidden = NO;
}


#pragma mark -父类方法子类实现
-(void)getPluginList:(NSArray *)array{
    

    
    

}


@end
